//////////////////////////////////////////////////////////////////////////
///	\file:	MThread.h
///	Copyright (MingYou) 2009 - All Rights Reserved
///	Author:	Robert.Ma
///	Date:	[2009/04/28]
///	Description:
///
//////////////////////////////////////////////////////////////////////////
#pragma once

//	Count value.
const DWORD dwMaxThreadTerminationWaitCount	= 5;
const DWORD dwThreadWaitTime				= 1000;

class CMMutex
{
public:
	CMMutex(HANDLE hObject);
	~CMMutex();

	bool Lock(DWORD dwWait);

	void UnLock();

private:
	HANDLE	m_hObject;
};

class CMThread
{
public:
	CMThread(void);
	virtual ~CMThread(void);

	HANDLE	Create(DWORD* lpID = NULL);
	void	Close();

	HANDLE	GetThreadHandle() const;
	DWORD	GetThreadID() const;
	DWORD	GetThreadErrorID() const;

	static BOOL SafePostMessage(DWORD idThread,
	                            UINT Msg,
	                            WPARAM wParam = 0,
	                            LPARAM lParam = 0);

protected:
	static DWORD WINAPI Run(LPVOID pParam);

	virtual DWORD Execute() PURE;
	virtual void PreCreate() PURE;
	virtual void AfterCreate() PURE;
	virtual void PreClose() PURE;
	virtual void AfterClose() PURE;

	HANDLE	m_hThread;
	DWORD	m_dwThreadID;
	DWORD	m_dwErrorID;
};
